package com.icss.ah.authority.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icss.ah.base.bean.SyncSelectTree;
import com.icss.j2ee.util.UUID;

@Service
@Transactional
public class AuthorityService {
	@Autowired
	private SqlSessionTemplate sqlSessionTemplate;
	
	/**
	 * 获得当前店铺店员信息
	 * @param map
	 * @return
	 */
	public List<Map> queryAccountList(HashMap map) {
		List<Map> list = new ArrayList<Map>();
		try {
			list = sqlSessionTemplate.selectList("AuthorityBean.queryAccountList",map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list == null ? new ArrayList<Map>() : list;
	}
	
	/**
	 * 获得当前店铺店员数量
	 * @param map
	 * @return
	 */
	public Long queryAccountCount(HashMap map) {
		Long reValue = 0l;
		try {
			reValue = (Long)sqlSessionTemplate.selectOne("AuthorityBean.queryAccountCount",map);
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		return reValue;
	}
	
	/**
	 * 获取公众号资源树数据
	 */
	public List getResourceTree(SyncSelectTree selectTree) {
		String id = selectTree.getId();//id 异步加载展开树时传到后台的id
		String ifAll = selectTree.getIfAll();//是否一次性展示所有数据，1是 0否
		List list = new ArrayList();
		if (ifAll != null && !"".equals(ifAll)) {
			if ("0".equals(ifAll)) {//展开树时加载数据
				if ("".equals(id)) {//如果查询id没有值，表示查询根节点
					list = sqlSessionTemplate.selectList("AuthorityBean.getResourceTree",selectTree);
				}else{//如果查询id与入口编码编码不相等，表示查询根节点下的子节点
					list = sqlSessionTemplate.selectList("AuthorityBean.getResourceTreeChild",selectTree);
				}
			}else{//一次加载所有数据
				if ("".equals(id)) {//如果查询id没有值，表示查询根节点
					list = sqlSessionTemplate.selectList("AuthorityBean.getResourceTreeAll",selectTree);
				}else{//如果查询id与入口编码编码不相等，表示查询根节点下的子节点
					list = sqlSessionTemplate.selectList("AuthorityBean.getResourceTreeChild",selectTree);
				}
			}
		}
		return list == null ? new ArrayList() : list;
	}
	
	public List queryCheckList(HashMap map) {
		List list = new ArrayList();
		try {
			list = sqlSessionTemplate.selectList("AuthorityBean.queryCheckList",map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list == null ? new ArrayList() : list;
	}
	
	public List queryCheckUserList(HashMap map) {
		List list = new ArrayList();
		try {
			list = sqlSessionTemplate.selectList("AuthorityBean.queryCheckUserList",map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list == null ? new ArrayList() : list;
	}

	/**
	 * 分配账号权限
	 * @param map
	 * @return
	 */
	public boolean insertRigthUser(HashMap map) {
		boolean flag = false;
		try {
			String uuidStr = map.get("uuidStr").toString();
			if(uuidStr != null && !"".equals(uuidStr)){
				sqlSessionTemplate.selectList("AuthorityBean.deleteRigthUser", map);
				String uuids[] = uuidStr.split(",");
				for (String ids : uuids) {
					map.put("uuid", new UUID().toString());
					map.put("resUuid", ids);
					sqlSessionTemplate.selectList("AuthorityBean.insertRigthUser", map);
				}
			}
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
}
